const Gaojing = require("../model/gaojing.model.js");
const { getGaojing } = require("../common/common.js");
const { getOptical } = require("../services/opticals.service.js");
const { Op } = require("sequelize")
class GaojingService {
    //保存告警
    async saveGaojings({ gaojingId, linkId, linkName, lineName, status, statusValue, times, works, cancat, origin, originId, finishing, finishingId, distance, gaojingType, endtimes, longitude, latitude,X,Y }) {
        try {
            var update = false;
            var gaojing = {};
            var whereOpt = {};
            if (gaojingId > 0) {
                update = true;
                whereOpt.gaojingId = gaojingId;
                let dt = await Gaojing.findOne({ where: whereOpt });
                if (!dt || dt == "" || dt == null || dt == undefined) {
                    return false;
                } else {
                    gaojing = JSON.parse(JSON.stringify(dt));
                }
            }
            if (works) {
                works = JSON.stringify(works)
            } else {
                works = "[]"
            }
            linkId >= 0 && Object.assign(gaojing, { linkId });
            gaojingType >= 0 && Object.assign(gaojing, { gaojingType });
            linkName && Object.assign(gaojing, { linkName });
            lineName && Object.assign(gaojing, { lineName });
            status && Object.assign(gaojing, { status });
            statusValue && Object.assign(gaojing, { statusValue });
            times = Math.floor(new Date().getTime() / 1000);
            times && Object.assign(gaojing, { times });
            works && Object.assign(gaojing, { works });
            cancat && Object.assign(gaojing, { cancat });
            origin && Object.assign(gaojing, { origin });
            originId && Object.assign(gaojing, { originId });
            finishing && Object.assign(gaojing, { finishing });
            finishingId > 0 && Object.assign(gaojing, { finishingId });
            distance && Object.assign(gaojing, { distance });
            X > 0 && Object.assign(gaojing, { X });
            Y > 0 && Object.assign(gaojing, { Y });
            longitude && Object.assign(gaojing, { longitude });
            latitude && Object.assign(gaojing, { latitude });
            if (update) {
                endtimes = Math.floor(new Date().getTime() / 1000);
            }
            endtimes > 0 && Object.assign(gaojing, { endtimes });
            if (Object.keys(gaojing).length == 0) {
                return false;
            }
            let ps = await getOptical({ linkName });
            if (!ps || !(ps instanceof Array) || ps.length == 0) {
                return false;
            }
            if (status == "正常" && statusValue == "") {
                //解除告警
                gaojing.status = status;
            } else {
                //设置告警
                let gj;
                if (latitude && longitude) {
                    gj = getGaojing(ps, statusValue, longitude, latitude);
                    if (gj.code != 0) {
                        return false;
                    }
                    gaojing.origin = gj.data.origin;
                    gaojing.originId = gj.data.originId;
                    gaojing.finishing = gj.data.finishing;
                    gaojing.finishingId = gj.data.finishingId;
                    gaojing.distance = gj.data.distance;
                    if(gj.data.latitude){
                        gaojing.latitude = gj.data.latitude;
                    }
                    if(gj.data.longitude){
                        gaojing.longitude = gj.data.longitude;
                    }
                    if(gj.data.x){
                        gaojing.X = gj.data.x;
                    }
                    if(gj.data.y){
                        gaojing.Y = gj.data.y;
                    }
                }
            }
            var res;
            if (update) {
                res = await Gaojing.update(gaojing, { where: whereOpt });
                if (res[0] > 0) {
                    gaojing.gaojingId = gaojingId;
                    return JSON.parse(JSON.stringify(gaojing));
                } else {
                    return false;
                }
            } else {
                res = await Gaojing.create(gaojing);
                if (res.dataValues == undefined) {
                    return false;
                } else {
                    return JSON.parse(JSON.stringify(res.dataValues));;
                }
            }
        } catch (error) {
            console.log(error);
            return null;
        }
    }
    async getGaojings({ gaojingId, linkId, linkName, gaojingType }) {
        try {
            var whereOpt = {};
            linkId > 0 && Object.assign(whereOpt, { linkId });
            gaojingType >= 0 && Object.assign(whereOpt, { gaojingType });
            gaojingId > 0 && Object.assign(whereOpt, { gaojingId });
            linkName && Object.assign(whereOpt, { linkName });
            var res;
            if (gaojingId > 0) {
                res = await Gaojing.findOne({ where: whereOpt });
            } else {
                res = await Gaojing.findAll({ where: whereOpt, order: [["gaojingId", "ASC"]] })
            }
            if (!res) {
                if (gaojingId > 0) {
                    return {}
                } else {
                    return []
                };
            } else {
                return JSON.parse(JSON.stringify(res));
            }
        } catch (error) {
            console.log(error);
            return null;
        }
    }
    //查询未解除的告警
    async getTerminalGaojing({ linkName, gaojingType, endtimes }) {
        try {
            if (!linkName || linkName == "" || linkName == undefined) {
                return false;
            }
            if (!gaojingType || gaojingType == "" || gaojingType == undefined) {
                gaojingType = 0;
            } else {
                gaojingType = 1;
            }
            /* if(!endtimes || endtimes == 0 || endtimes == undefined){
                return false;
            }
            let res = Gaojing.findAll({
                where:{
                    [Op.eq]:[{linkName,gaojingType}],
                    linkName:{
                        [Op.eq]:linkName
                    },
                    gaojingType:{
                        [Op.eq]:gaojingType
                    },
                    endtimes:{
                        [Op.gt]:0
                    }
                },
                order:[
                    ['gaojingId','DESC']
                ]
            }) */
            let res = await Gaojing.findAll({
                where: { linkName, gaojingType },
                order: [
                    ['gaojingId', "DESC"]
                ]
            })
            if (!res) {
                return [];
            } else {
                var list = JSON.parse(JSON.stringify(res));
                if (list instanceof Array && list.length > 0) {
                    return list[0];
                }
                return false;
            }
        } catch (error) {
            console.log(error);
            return false;
        }
    }
}
module.exports = new GaojingService();